import 'package:flutter/material.dart';
import 'package:flutterproject/page/mine/children/assets/childdren/formatter.dart';
import 'package:flutterproject/util/address_manager.dart';
import 'package:flutterproject/util/dialog_toast/dialog_util.dart';
import 'package:flutterproject/util/dialog_toast/toast_util.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/http/http_client.dart';
import 'package:flutterproject/util/http/http_exception.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/widget/app_bar_common.dart';
import 'package:flutterproject/widget/button_common.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:flutterproject/widget/text/text_field_common.dart';
import 'package:get/get.dart';

class ChargePage extends StatefulWidget {
  final String barTitle;
  final String title;
  final String left;
  final String right;
  final String btnStr;
  final String bgImage;
  final TextEditingController? controller;
  final Color btnColor;
  final Color inputBgColor;
  final VoidCallback callback;
  final int? walletId;

  const ChargePage(
      {super.key,
      required this.barTitle,
      required this.title,
      required this.left,
      required this.right,
      required this.btnStr,
      required this.bgImage,
      required this.controller,
      required this.btnColor,
      required this.inputBgColor,
      required this.callback,
      this.walletId});

  @override
  State<ChargePage> createState() => _ChargePageState();
}

class _ChargePageState extends State<ChargePage> {
  final textController = TextEditingController();
  final focusNode = FocusNode();
  bool hasModify = false;

  @override
  void initState() {
    super.initState();
    (widget.controller ?? textController).text = '0';
    // (widget.controller ?? textController).addListener(() {
    //   print("koma===1:${(widget.controller ?? textController).text}");
    //   (widget.controller ?? textController).text =
    //       "${double.tryParse((widget.controller ?? textController).text) ?? "0"}";
    // });
  }

  @override
  Widget build(BuildContext context) {
    return ColoredBox(
      color: Color(0xFFF6F6F6),
      child: Column(
        children: [
          AppBarCommon(
            title: widget.barTitle,
            bgColor: Color(0xFFEDC88B),
            leftColor: Colors.white,
            titleColor: Colors.white,
          ),
          Container(
            margin: MarginPaddingUtil.symmetric(horizontal: 40, vertical: 64),
            padding: MarginPaddingUtil.fromLTRB(
                left: 30, top: 167, right: 30, bottom: 58),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("assets/image/${widget.bgImage}.png"),
                fit: BoxFit.fill,
              ),
            ),
            width: MediaQuery.of(context).size.width,
            child: Column(
              children: [
                TextCommon(
                  widget.title,
                  size: 28.sp,
                  fontWeight: FontWeight.bold,
                  color: Colors.white,
                ),
                30.heightBox,
                Container(
                  padding: MarginPaddingUtil.fromLTRB(
                      left: 20, top: 40, right: 0, bottom: 32),
                  decoration: BoxDecoration(
                      color: widget.inputBgColor,
                      borderRadius: BorderRadius.circular(20.radius)),
                  child: Row(
                    children: [
                      Expanded(
                        child: Row(
                          children: [
                            TextCommon(
                              "从",
                              size: 28.sp,
                              color: Colors.white.withOpacity(0.5),
                            ),
                            Expanded(
                              child: TextCommon(
                                widget.left,
                                size: 28.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                align: TextAlign.center,
                              ),
                            ),
                          ],
                        ),
                      ),
                      ImageCommon(
                        "/image/ic_change",
                        size: 48.width,
                      ),
                      Expanded(
                        child: Row(
                          children: [
                            42.widthBox,
                            TextCommon(
                              "至",
                              size: 28.sp,
                              color: Colors.white.withOpacity(0.5),
                            ),
                            Expanded(
                              child: TextCommon(
                                widget.right,
                                size: 28.sp,
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                align: TextAlign.center,
                              ),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
                50.heightBox,
                Container(
                  padding: MarginPaddingUtil.fromLTRB(
                      left: 20, top: 24, right: 0, bottom: 24),
                  decoration: BoxDecoration(
                      color: widget.inputBgColor,
                      borderRadius: BorderRadius.circular(20.radius)),
                  child: Row(
                    children: [
                      TextCommon(
                        "输入额度",
                        size: 28.sp,
                        color: Colors.white.withOpacity(0.5),
                      ),
                      Expanded(
                        child: TextFieldCommon(
                          align: TextAlign.right,
                          controller: widget.controller ?? textController,
                          height: 72.width,
                          size: 50.sp,
                          textColor: Colors.white,
                          padding: MarginPaddingUtil.symmetric(horizontal: 25),
                          keyboardType: TextInputType.number,
                          focusNode: focusNode,
                          inputFormatters: [InputFormatter()],
                          onChanged: (value) {
                            // if (value == "") {
                            //   (widget.controller ?? textController).text = "0";
                            // }
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                76.heightBox,
                ButtonCommon(
                  text: widget.btnStr,
                  onTap: () => widget.controller == null
                      ? showPwdDialog()
                      : widget.callback.call(),
                  width: 472.width,
                  color: Colors.white,
                  borderRadios: 30.radius,
                  fontColor: widget.btnColor,
                  fontSize: 36.sp,
                  fontWeight: FontWeight.bold,
                  padding: MarginPaddingUtil.symmetric(vertical: 20),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  HttpClient client = Get.find<HttpClient>();

  /// 充值
  Future charge(String pwd) async {
    try {
      DialogUtil.showLoading();
      await client.post(
        AddressManager.charge,
        data: {
          "value": (widget.controller ?? textController).text,
          "walletId": widget.walletId,
          "password": pwd,
        },
      );
      ToastUtil.showSuccess("充值成功!");
      Get.back();
      return true;
    } on HttpException catch (e) {
      ToastUtil.showFailed(e.message);
      return false;
    } finally {
      DialogUtil.dismiss();
    }
  }

  void showPwdDialog() {
    DialogUtil.showPwdDialog((value) {
      charge(value);
    });
  }
}
